package com.sjz.adapter;

import java.util.List;

import com.sjz.db.R;
import com.sjz.domain.Person;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


/****
 * 自定义类实现BaseAdapter来展示数据
 * @author Administrator
 *
 */
public class PersonAdapter extends BaseAdapter {
	private List<Person> persons;//在绑定的数据
	private int resource;//绑定的条目界面
	private LayoutInflater inflater;  //布局填充器   可以使用item文件生成对应的view对象
	
	public PersonAdapter(Context context, List<Person> persons, int resource) {
		this.persons = persons;
		this.resource = resource;
		//通过上下文 对象来得到系统内置的布局填充器对象
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
    
	public int getCount() {
		return persons.size();//数据总数
	}

	public Object getItem(int position) {
		return persons.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	/****
	 * 优化版本
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		TextView nameView = null;
		TextView phoneView = null;
		TextView amountView = null;
		if(convertView==null){   //如果是第一页的话，创建一个对象  只初始化一次
			convertView = inflater.inflate(resource, null);//生成条目界面对象      resource 指定哪一个文件来生成view对象
			nameView = (TextView) convertView.findViewById(R.id.name);  //得到三个显示对象 
			phoneView = (TextView) convertView.findViewById(R.id.phone);
			amountView = (TextView) convertView.findViewById(R.id.amount);
			
			ViewCache cache = new ViewCache();//缓存赋值
			cache.nameView = nameView;
			cache.phoneView = phoneView;
			cache.amountView = amountView;			
			convertView.setTag(cache);
		}else{//不是第一次的话，直接通过缓存中来得到
			ViewCache cache = (ViewCache) convertView.getTag();
			nameView = cache.nameView;
			phoneView = cache.phoneView;
			amountView = cache.amountView;
		}
		Person person = persons.get(position);//得到要绑定的数据
		//下面代码实现数据绑定
		nameView.setText(person.getName());
		phoneView.setText(person.getPhone());
		amountView.setText(person.getAmount().toString());
		
		return convertView;
	}
 
	/****
	 * 基础版本  
	 */
	public View getViewBase(int position, View convertView, ViewGroup parent) {
		TextView nameView = null;
		TextView phoneView = null;
		TextView amountView = null;
		if(convertView==null){   //如果是第一页的话，创建一个对象
			convertView = inflater.inflate(resource, null);//生成条目界面对象      resource 指定哪一个文件来生成view对象
		} 
		nameView = (TextView) convertView.findViewById(R.id.name);  //得到三个显示对象 
		phoneView = (TextView) convertView.findViewById(R.id.phone);
		amountView = (TextView) convertView.findViewById(R.id.amount);
		
		Person person = persons.get(position);//得到要绑定的数据
		 
		//下面代码实现数据绑定
		nameView.setText(person.getName());
		phoneView.setText(person.getPhone());
		amountView.setText(person.getAmount().toString());
		
		return convertView;
	}
	
	
	/***
	 * 内部类 缓存
	 * @author Administrator
	 *
	 */
	private final class ViewCache{
		public TextView nameView;
		public TextView phoneView;
		public TextView amountView;
	}

}